const fs = require('fs')
const cheerio = require('cheerio')
const axios = require('axios')
const iconv = require('iconv-lite')
const mysql = require('mysql')



var pool = mysql.createPool({
    connectionLimit: 10,
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'noval',
    port: '3306'
});


var arrBuffer = fs.readFileSync('./url.json')
var arr = JSON.parse(arrBuffer.toString())
for (var i = 0; i < arr.length; i++) {
    var book = {}
    axios({
        method: 'get',
        url: 'https://www.81book.com' + arr[i],
        responseType: 'stream'
    }).then(res => {
        var stream = []
        res.data.on('data', st => {
            stream.push(st)
        })
        res.data.on('end', () => {
            let buffer = Buffer.concat(stream)
            let domStr = iconv.decode(buffer, 'gbk')
            var $ = cheerio.load(domStr)
            book.url = $('meta[property="og:url"]').attr('content')
            book.bookName = $('#info h1').text()
            book.author = $('#info p').eq(0).text().slice(7)
            book.intro = $('#intro p').eq(0).text()
            book.category = $('.con_top a').eq(1).text()
            book.status = $('#info p').eq(1).text().slice(7, 10)
            book.updateTime = $('#info p').eq(2).text().slice(5)
            book.lastChapterName = $('#info p').eq(3).find('a').text()
            book.lastChapterUrl = 'https://www.81book.com/' + $('#info p').eq(3).find('a').attr('href')
            book.picUrl = $('#fmimg img').attr('src')
                // INSERT INTO `rank` VALUES (),为啥要这样才能插入数据，卧槽
            var queryStr = `insert into ` + "`book`" + ` values ('${book.url}','${book.bookName}','${book.intro}','${book.category}','${book.status}','${book.author}','${book.lastChapterName}','${book.lastChapterUrl}','${book.picUrl}','${book.updateTime}')`;
            // console.log(queryStr)
            pool.query(queryStr, (err, result) => {
                if (err) {
                    console.log('存储失败' + err)
                } else {
                    console.log('存储成功')
                }
            })
        })
    })
}